package cn.edu.neusoft.yyd208.foodorder.activity;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.squareup.picasso.Picasso;

import java.util.List;

import cn.edu.neusoft.yyd208.foodorder.R;
import cn.edu.neusoft.yyd208.foodorder.adapter.CommentFoodAdapter;
import cn.edu.neusoft.yyd208.foodorder.bean.CollectBean;
import cn.edu.neusoft.yyd208.foodorder.bean.CommentFoodBean;
import cn.edu.neusoft.yyd208.foodorder.bean.FoodBean;
import cn.edu.neusoft.yyd208.foodorder.bean.RegisterBean;
import cn.edu.neusoft.yyd208.foodorder.listener.ListListener;
import cn.edu.neusoft.yyd208.foodorder.listener.TListener;
import cn.edu.neusoft.yyd208.foodorder.model.AddCommentModel;
import cn.edu.neusoft.yyd208.foodorder.model.CollectCatModel;
import cn.edu.neusoft.yyd208.foodorder.model.CommentFoodModel;
import cn.edu.neusoft.yyd208.foodorder.model.FoodInfoModel;


public class FoodInfoActivity extends BaseActivity {
    String id,foodid,userid,flag;
    int flag1;
    Context context;
    ImageView reback, collect;
    TextView tv_id,tv_intro,tv_price,tv_foodid;
    EditText et_addcomment;
    Button btn_purch,bt_scomment;
    ImageView img_food;
    List<CommentFoodBean> items;
    RecyclerView recyclerView;
    FoodInfoModel foodInfoModel;
    CollectCatModel collectCatModel;
    CommentFoodModel commentFoodModel;
    ListListener<CommentFoodBean> commentFoodListener=new ListListener<CommentFoodBean>() {
        @Override
        public void onResponse(List<CommentFoodBean> list) {
            items=list;
            CommentFoodAdapter myAdapter=new CommentFoodAdapter(FoodInfoActivity.this,items,R.layout.comment);
            recyclerView.setAdapter(myAdapter);
            recyclerView.setItemAnimator(new DefaultItemAnimator());

        }

        @Override
        public void onFail(String msg) {

        }
    };
    TListener<RegisterBean> addCommentListener=new TListener<RegisterBean>() {
        @Override
        public void onResponse(RegisterBean registerBean) {
            if(registerBean.getSuccess().equals("1")){
                showToast("评论成功");
            }
            else {
                showToast("评论失败");
            }
        }

        @Override
        public void onFail(String msg) {

        }
    };
    TListener<FoodBean> tListener=new TListener<FoodBean>() {
    @Override
    public void onResponse(FoodBean foodBean) {



    }

    @Override
    public void onFail(String msg) {

    }
};

    TListener<RegisterBean> collectListListener=new TListener<RegisterBean>() {
        @Override
        public void onResponse(RegisterBean registerBean) {
            if(registerBean.getSuccess().equals("1")){
                if(flag1==1){
                    collect.setImageResource(R.drawable.collect);
                    flag1=0;
                }
                else {
                    collect.setImageResource(R.drawable.ycollect);
                    flag1=1;
                }
                showToast("操作成功");

            }
            else {
                showToast("操作失败");
            }
        }

        @Override
        public void onFail(String msg) {

        }
    };
    TListener<CollectBean> judgeListener=new TListener<CollectBean>() {
        @Override
        public void onResponse(CollectBean collectBean) {
            flag = collectBean.getCollected().toString();
            if (flag.equals("1")) {
                flag1 = 1;
                collect.setImageResource(R.drawable.ycollect);
                showToast("已收藏");
            }
            else {
                flag1=0;
                collect.setImageResource(R.drawable.collect);
                showToast("未收藏");
            }
        }
        @Override
        public void onFail(String msg) {

        }
    };
    @Override
    void initView() {
        setLayout_file(R.layout.activity_food_info);
        reback = (ImageView) findViewById(R.id.reback);
        collect = (ImageView) findViewById(R.id.collect);
        tv_id=(TextView)findViewById(R.id.tv_id);
        //tv_foodid=(TextView)findViewById(R.id.tv_foodid);
        tv_intro=(TextView)findViewById(R.id.tv_intro);
        tv_price=(TextView)findViewById(R.id.tv_price);
        et_addcomment=(EditText)findViewById(R.id.et_addcomment);
        btn_purch=(Button) findViewById(R.id.btn_purch);
        bt_scomment=(Button)findViewById(R.id.bt_scomment);
        img_food=(ImageView)findViewById(R.id.img_food);
        Intent intent = getIntent();
        tv_id.setText(intent.getStringExtra("foodname"));
        tv_intro.setText(intent.getStringExtra("foodintro"));
        tv_price.setText(intent.getStringExtra("foodprice"));
        Picasso.with(context).load("http://172.24.10.175:8080/foodService"+ intent.getStringExtra("foodimg")).into(img_food);
        userid=getUser_userid();
        foodid = intent.getStringExtra("foodid");

        recyclerView =(RecyclerView) findViewById(R.id.ctrecyclerview);
        LinearLayoutManager linearLayoutManager=new LinearLayoutManager(this);
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(linearLayoutManager);
    }

    @Override
    void initEvent() {
        reback.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        collect.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                collectCatModel.collect(userid,foodid,collectListListener);
            }
        });
        btn_purch.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String foodname=tv_id.getText().toString();
                String foodprice=tv_price.getText().toString();
                Intent intent=new Intent(FoodInfoActivity.this,PurchActivity.class);
                intent.putExtra("foodname",foodname);
                intent.putExtra("foodprice",foodprice);
                startActivityForResult(intent,1);
            }
        });
        bt_scomment.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String content=et_addcomment.getText().toString();
                Intent intent = getIntent();
                String orderid = intent.getStringExtra("orderid");
                CommentFoodModel commentFoodModel=new CommentFoodModel();
                commentFoodModel.getFoodMessage(foodid,commentFoodListener);
                AddCommentModel addCommentModel=new AddCommentModel();
                addCommentModel.getLoginMessage(orderid,content,addCommentListener);
                addCommentModel.getLoginMessage("1",content,addCommentListener);
            }
        });
    }

    @Override
    void initData() {
        foodInfoModel=new FoodInfoModel();
        foodInfoModel.getFoodInfoMessage(id,tListener);

        collectCatModel=new CollectCatModel();
        collectCatModel.judge(userid,foodid,"1",judgeListener);

        CommentFoodModel commentFoodModel=new CommentFoodModel();
        commentFoodModel.getFoodMessage(foodid,commentFoodListener);
    }

}
